<template>
  <div class="">
    <div class="login-container">
      <el-row>
        <el-col :span="24" :offset="0">
          <h1 style="padding: 80px 0; color: #409eff;">灵犀测试平台</h1>
          <el-form
          ref="loginForm" 
          :model="loginForm"
          label-width="80px" 
          class='login-form' 
          :rules="rules"
          >
            <el-form-item class="el-input-item" label="手机号" prop="username">
              <el-input v-model="loginForm.username"></el-input>
            </el-form-item>
            <el-form-item class="el-input-item" label="密码" prop="password">
              <el-input v-model="loginForm.password" type="password"></el-input>
            </el-form-item>
            <el-form-item class="el-input-item">
              <el-button type="primary" @click="onSubmit" style="float: right;">
                登录
              </el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
  </div>
</template>



<script>

export default {
  name: "",
  data() {
    let validateTel = (rule, value, callback) => {
      if(!value.match(/^1[0-9]{10}$/)){
        callback(new Error("请输入正确的手机号码！"))
      }else{
        callback()
      }
    }
    return {
      loginForm: {
        username: "",
        password: ""
      },
      rules: {
        username: [
          {required: true,message: "请输入手机号码！",trigger: "blur"},
          {validator: validateTel,trigger: "blur"}
        ],
        password: [
          {required: true,message: "请输入密码！",trigger: "blur"},
          {min: 6, max: 20,message: "密码字符应该在6-20之间！",trigger: "blur"},
        ]
      }
    }
  },
  components: {},
  methods: {
    onSubmit(){
      this.$refs['loginForm'].validate(valid => {
        if(!valid){
          return;
        }
        console.log(this.loginForm)
        this.$http.login(this.loginForm).then(res => {/*  */
          console.log(res)
          const data = res.data
          const token = data.token;
          const user = data.user
          this.$auth.setUserToken(user,token)
          this.$router.push('/')
        })
      })
    }
  }
}
</script>



<style>
body{
  background: #fff;
}
.login-container {
    text-align: center;
    background-color: #fff;
    border-radius: 20px;
    width: 300px;
    height: 350px;
    margin: auto;
    position:fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    height: 500px;
    width: 500px;
    border: 1px solid #409eff;
    box-shadow: rgba(0, 0, 0, 0.12) 0px 2px 4px, rgba(0, 0, 0, 0.04) 0px 0px 6px;
}

.el-input-item {
    /* border: 1px solid tomato; */
    padding: 15px 90px 5px 90px;
}
</style>